package com.ptszyxx.popose.module.main.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.databinding.FragmentHomeSearchBinding;
import com.ptszyxx.popose.module.main.home.adapter.HomeSearchAdapter;
import com.ptszyxx.popose.module.main.home.vm.HomeSearchVM;
import com.ysg.base.BaseFragment;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.http.data.entity.home.HomeSearchEntity;

import java.util.List;

/**
 *
 */
public class HomeSearchFragment extends BaseFragment<FragmentHomeSearchBinding, HomeSearchVM> {
    private HomeSearchAdapter adapter;

    public HomeSearchFragment(){}

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_home_search;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public HomeSearchVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(HomeSearchVM.class);
    }

    @Override
    public void initAdapter() {
        super.initAdapter();
        adapter = new HomeSearchAdapter(viewModel);
        binding.refreshView.initAdapter(adapter);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onRefreshEvent.observe(this, new Observer<List<HomeSearchEntity>>() {
            @Override
            public void onChanged(List<HomeSearchEntity> list) {
                setAdapterUI(list);
            }
        });
    }

    private void setAdapterUI(List<HomeSearchEntity> list) {
        binding.refreshView.setAdapterUI(adapter, list, viewModel.isRefresh());
    }
}
